
************************************************************************************;

/* 					This file provides codes for Panel A of Table 3					*/
/*			Regressions of SYY on lagged HF and MF flows, both MA and changes 		*/

**************************************************************************************;

libname rep "C:\Users\~";

data regbase; set rep.mydata; run;


data regbase; set regbase; 
lhfflow_ma = lag1(hfflow_ma); 
lactive_ma = lag1(active_ma); 
lhfflow_chg = lag1(hfflow_chg); 
lactive_chg = lag1(active_chg); 
lmktrf = lag1(mktrf); 
lamihud = lag1(amihud); 
ltover = lag1(tover); 
lhml = lag1(hml);
lsmb = lag1(smb); 
run;

*** Table 3 Panel A: Betas and t values ***;

option missing = " ";
%macro print;
data N; set N; Estimate = nValue1; if Label1 = "Used"; run;
data Nobs; set N; Parameter = "N"; keep EstType Parameter Estimate; run;
data r2; set fit; Parameter = "RSQ"; keep EstType Parameter RSquare; rename RSquare = Estimate;  run;
data final; set parest Nobs r2 ; drop EstType StdErr Probt DF; run;
data final&i; set final; rename Estimate = &fvar tValue = t_&i; 
data final&i; set final&i;
if Parameter not in ("f", "N", "RSQ") then delete;
%mend;

%let var1 = lhfflow_ma;
%let var2 = lhfflow_chg;
%let var3 = lactive_ma;
%let var4 = lactive_chg;

%macro decomp ;

%do i=1 %to 4;
%let fvar = &&var&i;
%put &fvar;

ods output "Nonlinear GMM Summary of Residual Errors" = fit;
ods output "Nonlinear GMM Parameter Estimates" = parest;
ods output "Estimation Summary Statistics" = N;
proc model data=regbase plot =none;
endo syy ; exog &fvar lmktrf lamihud ltover lhml lsmb; ; instruments _exog_;
syy =b0 + f * &fvar +b1*lmktrf +b2*lamihud +b3*ltover +b4*lhml +b5*lsmb; ;
fit syy / gmm kernel=(bart,13,0) vardef=n; run; quit; 
%print;
%end;

data T3A; merge final1 - final4; run;
%mend;

%decomp ;

proc print data = T3A noobs; run;

